#pragma once
#include<iostream>
#include<assert.h>
#include<string>
using namespace std;


namespace cyq
{
	template <class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator begin()const
		{
			return _start;
		}

		const_iterator end() const
		{
			return _finish;
		}

		vector()
			:_start(nullptr)
			,_finish(nullptr)
			,_endofstorage(nullptr)
		{}

		//vector(size_t n,const T& val=T())
		vector(int n, const T& val = T())
		{
			resize(n, val);
		}

		//用其他类型的迭代器去构造
		template <class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}


		//拷贝构造
		vector(const vector<T>& v)
			:_start(nullptr)
			, _finish(nullptr)
			, _endofstorage(nullptr)
		{
			_start = new T[v.capacity()];
			//memcpy(_start, v._start, sizeof(T) * v.size());
			for (size_t i = 0; i < v.size(); i++)
			{
				_start[i] = v._start[i];
			}
			_finish = _start + v.size();
			_endofstorage = _start + v.capacity();
		}

		/*vector(const vector<T>& v)
		{
			reserve(v.capacity());
			for (auto e : v)
			{
				push_back(e);
			}
		}*/

		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_endofstorage, v._endofstorage);
		}

		//赋值
		vector<T>& operator=(vector<T> v)
		{
			swap(v);

			return *this;
		}

		
		~vector()
		{
			if (_start)
			{
				delete[] _start;
				_start = _finish = _endofstorage = nullptr;
			}
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t sz = size();
				T* temp = new T[n];
				if (_start)
				{
					//memcpy(temp, _start, sizeof(T) * sz);
					for (size_t i = 0; i < size(); i++)
					{
						temp[i] = _start[i];
					}

					delete[] _start;
				}
	
				_start = temp;
				_finish = _start + sz;
				_endofstorage = _start + n;
			}
		}

		void push_back(const T& x)
		{
			/*if (_finish == _endofstorage)
			{
				size_t newcapacity = capacity() == 0 ? 4 : capacity()*2;
				reverse(newcapacity);
			}

			*_finish = x;
			_finish++;*/

			insert(end(), x);
		}

		int capacity() const
		{
			return _endofstorage - _start;
		}

		int size() const
		{
			return _finish - _start;
		}

		T& operator[](int pos)
		{
			assert(pos < size());

			return _start[pos];
		}

		const T& operator[](int pos) const
		{
			assert(pos < size());

			return _start[pos];
		}

		iterator insert(iterator pos, const T& x)
		{
			assert(pos >= _start && pos <= _finish);
			if (_finish == _endofstorage)
			{
				size_t len = pos - _start;

				size_t newcapacity = capacity() == 0 ? 4 : capacity()*2;
				reserve(newcapacity);

				//解决pos引起的迭代器失效问题（扩容会使pos变成野指针）
				pos = _start + len;
			}
			iterator end = _finish - 1;
			while (end >= pos)
			{
				*(end + 1) = *end;
				end--;
			}
			*pos = x;
			_finish++;

			return pos;;
		}

		iterator erase(iterator pos)
		{
			assert(pos >= _start && pos < _finish);
			iterator temp = pos;

			while (pos < _finish)
			{
				*pos = *(pos + 1);
				pos++;
			}

			_finish--;

			return temp;
		}

		bool empty()
		{
			return _start == _finish;
		}

		void pop_back()
		{
			if (!empty())
			--_finish;
		}

		void resize(size_t n, const T& val = T())
		{
			//比size小
			if (n < size())
			{
				_finish = _start + n;
			}
			else
			{
				reserve(n);
				//填数据
				while (_finish < _start + n)
				{
					*_finish = val;
					_finish++;
				}
			}
		}

	private:
		iterator _start=nullptr;
		iterator _finish=nullptr;
		iterator _endofstorage=nullptr;
	};

}
